/*
 * SubServerListener.java
 *
 * Created on August 10, 2006, 11:02 PM
 *
 */
package org.letva.copie.mainserver;

import java.io.*;
import java.net.*;
import java.util.*;
import org.letva.copie.common.message.SubServerInfo;

/**
 *
 * @author Vuk Nikolic
 */
public class SubServerListener extends Thread {

	/** Creates a new instance of SubServerListener */
	public SubServerListener(MainServer ms) {
		super("SubServerListner");
		myMainServer = ms;
		myMainServer.writeLog(MainServer.getLabel("SUBSERVERLISTENER CREATED."));
	}

	@Override
	public void run() {
		// prvo da napravimo serverski socket
		try {
			serverSocket = new ServerSocket(myMainServer.getSubServerPort());
			// napravili smo serverski socket
		} catch (IOException e) {
			myMainServer.getMyWindow().setDialogException(MainServer.getLabel("CONNECTION ERROR"), MainServer.getLabel("CANNOT LISTEN PORT: ") + myMainServer.getSubServerPort());
			listening = false;
		}

		myMainServer.writeLog(MainServer.getLabel("SUBSERVERLISTENER: SERVERSOCKET CREATED."));
		// e sada da prihvatimo nove konekcije
		while (listening) {

			try {

				myMainServer.writeLog(MainServer.getLabel("SUBSERVERLISTENER: WAITING FOR A CONNECTION."));
				socket = serverSocket.accept(); // blokira se dok neko ne uspostavi vezu
				myMainServer.writeLog(MainServer.getLabel("SUBSERVERLISTENER: SUBSERVER CONNECTED:") + socket.getInetAddress());


				// cim je prosao accept znaci neko hoce da uspostavi vezu
				// prepoznajem ga po ip adresi
				String subServerUrl = socket.getInetAddress().toString();

				// da vidimo da li je taj vec prijavljen
				if (myMainServer.getSubServers().containsKey(subServerUrl) == false) {

					// ako nije dodajemo ga u listu prijavljenih
					SubServerInfo ssinfo = new SubServerInfo(subServerUrl, socket.getPort());
					myMainServer.addSubServer(ssinfo);

					// i da mu damo da radi
					Thread t = new SubServerThread(socket, myMainServer);
					myMainServer.incActiveSubServerThreads();

					t.start();

				} else {
					myMainServer.writeLog(MainServer.getLabel("SUBSERVERLISTENER: SUBSERVER ALREADY CONNECTED."));
					if (socket != null) {
						socket.close();
					}

				}

			} catch (SocketException ex) {
				myMainServer.getMyWindow().setDialogException(MainServer.getLabel("CONNECTION ERROR"), MainServer.getLabel("LOST CONNECTION WITH SUBSERVER"));
				myMainServer.writeLog(MainServer.getLabel("SUBSERVERLISTENER: LOST CONNECTION WITH SUBSERVER"));
			} catch (SocketTimeoutException ex) {
				myMainServer.getMyWindow().setDialogException(MainServer.getLabel("CONNECTION ERROR"), MainServer.getLabel("SUBSERVER TIMEOUT"));
				myMainServer.writeLog(MainServer.getLabel("SUBSERVERLISTENER: SUBSERVER TIMEOUT"));
			} catch (IOException e) {
				myMainServer.getMyWindow().setDialogException(MainServer.getLabel("CONNECTION ERROR"), MainServer.getLabel("PROBLEMS WITH INPUT/OUTPUT OPERATIONS"));
				myMainServer.writeLog(MainServer.getLabel("SUBSERVERLISTENER: PROBLEMS WITH INPUT/OUTPUT OPERATIONS"));
			}
		}

	}

	public void stopSubServerListener() {
		listening = false;
	}

	public Map<String, SubServerInfo> getSubServers() {
		return myMainServer.getSubServers();
	}

	public LinkedList getThreads() {
		return threads;
	}
	// soket ka novom subserveru
	private Socket socket = null;
	// referenca ka glavnom serveru
	private MainServer myMainServer;
	// za identifikaciju listnera
	private static int totalId = 0;
	private int id = ++totalId;
	// threadovi koji rade sa podserverima
	private LinkedList threads = new LinkedList();
	// serverski socket
	private ServerSocket serverSocket = null;
	// da li idalje nit da radi?
	boolean listening = true;
}
